//
//  WBMainTabbarViewController.swift
//  心情微博
//
//  Created by 孝飞王 on 16/12/20.
//  Copyright © 2016年 孝飞王. All rights reserved.
//

import UIKit

class WBMainTabbarViewController: UITabBarController {

    override func viewDidLoad() {
        
        super.viewDidLoad()
    
        setupChildControllers()
        setupCompause()
    }

    //MARK： 监听方法
    //FIXME:没实现
    
    
    lazy var compuse:UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
}



//类似OC中的分类  可以放到一个文件中便于代码维护
    //设置  所有自控制器
extension WBMainTabbarViewController {

    func setupCompause() {
        tabBar.addSubview(compuse)
        compuse.addTarget(self, action: #selector(compouseBtnClic), for: .touchUpInside)
        //计算按钮的宽度
        let width = CGFloat(childViewControllers.count)
        let w  = tabBar.frame.width/width - 1;
        compuse.frame = tabBar.bounds.insetBy(dx: 2 * w , dy: 0)
        
    }
    
    //监听方法
    @objc private func compouseBtnClic(){
        print("点击了按钮")
    }
    
          func setupChildControllers(){
            let array:[[String:String]]  = [
                                ["clsName":"WBHomeViewController","title":"首页","imageName":"home"],
                                ["clsName":"WBMessageViewController","title":"消息","imageName":"message_center"],
                                ["clsName":"","title":"","imageName":"home"],
                                ["clsName":"WBDiscoverViewController","title":"发现","imageName":"discover"],
                                ["clsName":"WBProfileViewController","title":"个人","imageName":"profile"]
            ]
            var arrayM = [UIViewController]()
            
            for  dict in array {
                arrayM.append(controller(dict: dict))
            }
            
            viewControllers = arrayM
        }
    
    private func controller(dict:[String:String])-> UIViewController{
        //取的字典的内容 
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let  cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
            else {
                return UIViewController();
        }
        
        let vc = cls.init()
        vc.title = title//tabbar_home_selected
        vc.tabBarItem.image = UIImage(named: "tabbar_" +  imageName)?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        let nv = WBNavigationController(rootViewController: vc)
        
        //设置字体大小  UIControlState(rawValue: 0)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted )
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 12)], for: UIControlState(rawValue: 0))
        return nv
    }
    
    
    }


















